package blog.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import blog.entity.Article;
import blog.entity.Category;
import blog.entity.Tag;
import blog.mapper.ArticleMapper;
import blog.service.ArticleService;
import blog.service.CategoryService;
import blog.service.TagService;

/**
 * 文章管理的服务层
 * @author 江佳洋
 *
 */
@Service
public class ArticleServiceImpl implements ArticleService{
	@Autowired
	private ArticleMapper articleMapper;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private TagService tagService;
	
	
	/**
	 * 查询最近的n条文章
	 */
	@Override
	public List<Article> listRecentArticle(Integer n) {
		return articleMapper.listRecentArticle(n);
	}
	
	/**
	 * 根据id查询文章的信息
	 */
	@Override
	public Article getArticleById(Integer getArticleById) {
		Article articleById = articleMapper.getArticleById(getArticleById);
		List<Category> articleCategory = categoryService.getArticleCategory(getArticleById);
		articleById.setCategoryList(articleCategory);
		List<Integer> articleTagId = tagService.getArticleTag(getArticleById);
		List<Tag> tagList = new ArrayList<>();
		if(tagList!=null) {
			for(Integer tagId:articleTagId) {
				tagList.add(tagService.selectTagById(tagId));
			}
		}
		articleById.setTagList(tagList);
		return articleById;
	}
	
	/**
	 * 利用分页插件对查询的所有数据分页去全部文章的主界面  并且含有分类，大分类小分类等功能
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public PageInfo<Article> getPageArticleList(Integer pageIndex, Integer pageSize ,String articleName,String articleStatus,
			String articleUpdateTime,String articleChildCategoryId,String articleParentCategoryId) {
		PageHelper.startPage(pageIndex,pageSize);
		List<Article> findAll = articleMapper.findAll(articleName,articleStatus,articleUpdateTime,articleChildCategoryId,articleParentCategoryId);
		PageInfo<Article> pageInfo = new PageInfo<Article>(findAll);
		for(Article page:pageInfo.getList()) {
			List<Category> categoryList = categoryService.getArticleCategory(page.getArticleId());
			page.setCategoryList(categoryList);
		}
		return pageInfo;
	}
	/**
	 * 添加文章信息
	 */
	@Override
	public void addArticle(Article article) {
		articleMapper.addArticle(article);
		for(Category cate:article.getCategoryList()) {
			categoryService.addArticleCategory(article.getArticleId(),cate.getCategoryId());
		}
		if(article.getTagList()!=null) {
			for(Tag tag:article.getTagList()) {
				tagService.addArticleTag(article.getArticleId(),tag.getTagId());
			}
		}
	}
	/**
	 * 根据id删除文章信息
	 * @param articleId 文章id
	 */
	public void deleteArticleById(Integer articleId) {
		articleMapper.deleteArticle(articleId);
		categoryService.deleteCategoryArticle(articleId);
		tagService.deleteTagArticle(articleId);
		
	}

	/**
	 * 修改文章信息
	 * @param article
	 */
	public void updateArticle(Article article) {
		articleMapper.updateArticle(article);
		categoryService.deleteCategoryArticle(article.getArticleId());
		tagService.deleteTagArticle(article.getArticleId());
		for(Category cate:article.getCategoryList()) {
			categoryService.addArticleCategory(article.getArticleId(),cate.getCategoryId());
		}
		if(article.getTagList()!=null) {
			for(Tag tag:article.getTagList()) {
				tagService.addArticleTag(article.getArticleId(),tag.getTagId());
			}
		}
	}

}
